Skip to content

Safe c interop #1071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Safe c interop #1071

wants to merge 2 commits into from

Conversation

hnrklssn
Copy link

@hnrklssn hnrklssn commented Jun 7, 2025

This adds documentation around __counted_by, __sized_by, API notes, and using lifetime attributes in C.
This is stacked on top of #980.

@rapidsna
Copy link

rapidsna commented Jun 7, 2025

CC @egorzhdan @Xazax-hun @DougGregor

@rauhul
Copy link
Member

rauhul commented Jun 7, 2025

Have you considered putting this documentation in a docc catalog?

@hnrklssn
Copy link
Author

hnrklssn commented Jun 7, 2025

Have you considered putting this documentation in a docc catalog?

I have not! I can't speak for Gábor and Egor, but I simply kept writing in the established file. What are the benefits of a docc catalog? I haven't contributed to the website before, so I don't really know how things work

These overloads provide the same bounds safety as their `UnsafeBufferPointer` equvalents, but with
added lifetime safety. If lifetime information is available the generated safe overload will always
choose to use `Span` - no `UnsafeBufferPointer` overload will be generated in this case. This means
that existing callers are not affected by annotating an API with `__counted_by`, but callers using the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there upstream documentation for these annotations in Clang? Do we want to link to that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are upstream docs for __counted_by, but they are outdated, since only a limited set of use cases are currently supported in upstream. The upstream docs currently only list support for flexible array members, which we don't import to Swift in the first place.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can refer to this documentation https://clang.llvm.org/docs/BoundsSafety.html

@rapidsna
Copy link

I remember the title was "Safe C++ and Swift interop" or something like that. If so we would also need to update it to include "C" as well.

source compatiblity, nor does it affect ABI. Instead it leverages bounds attributes to express the
pointer bounds in terms of other parameters in the function signature.

#### Annotating Pointers with Bounds Attributes

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This elaborates "UnsafeBufferPointer" in a great length, which is not the most recommended usage. I'd elaborate all these with "Span". Since lifetime annotations are already introduced for C++ span, you can just mention that it's still necessary without still focusing on the bounds annotations.

And then "UnsafeBufferPointer" can be briefly mentioned afterwards like -- if lifetime annotations are missing, it's imported as "UnsafeBufferPointer" instead of "Span".

hnrklssn added 2 commits June 20, 2025 13:35
This adds documentation around __counted_by, __sized_by, API notes, and
using lifetime attributes in C.
@hnrklssn
Copy link
Author

I'm having a bit of trouble deciding how to format the comparison between C and Swift; the narrow page size makes it hard to fit side-by-side comparisons. Following the approach used elsewhere on the page looks like this:
Screenshot 2025-06-20 at 1 16 37 PM
Code blocks cannot be used in markdown tables, so these have to be inline code snippets, which prevents formatting the code in a nicer way (as well as syntax highlighting).


Screenshot 2025-06-20 at 1 16 50 PM Using HTML tables we can get tables with code blocks. This gives us syntax highlighting, but unformatted it requires a fair bit of horizontal scrolling.
Screenshot 2025-06-20 at 1 21 22 PM With some formatting the situation improves, but they still don't fit without horizontal scrolling.
Screenshot 2025-06-20 at 1 31 41 PM We could instead skip the table and just use two separate code blocks before/after each other. Following the code formatting of the previous code blocks the C code looks like this, which works I guess, but doesn't look great right after each other imo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants